import { refreshToken } from "../../api/interface/auth";
class TokenManager {
  static getToken(): string | null {
    return localStorage.getItem("accessToken");
  }

  static setToken(token: string): void {
    localStorage.setItem("accessToken", token);
  }

  static removeToken(): void {
    localStorage.removeItem("accessToken");
  }

  static getRefreshToken(): string | null {
    return localStorage.getItem("refreshToken");
  }

  static setRefreshToken(token: string): void {
    localStorage.setItem("refreshToken", token);
  }

  static removeRefreshToken(): void {
    localStorage.removeItem("refreshToken");
  }

  static async refreshAccessToken(): Promise<string | null> {
    const currRefreshToken = TokenManager.getRefreshToken();
    if (currRefreshToken) {
      const response = await refreshToken({ refreshToken: currRefreshToken });
      if (response.success) {
        TokenManager.setToken(response.data.access);
        return response.data.access;
      }
    }
    return null;
  }
}

export default TokenManager;
